package ae.pcfc.etks.online.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.jboss.web.tomcat.security.login.WebAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.support.RequestContextUtils;

import ae.pcfc.etks.online.web.ConstantsAdmin;
import ae.pcfc.etks.online.web.PageType;
import ae.pcfc.etks.online.web.ViewPath;
import ae.pcfc.etks.online.web.ViewUtils;
import ae.pcfc.etks.online.web.model.ClientLogin;
import ae.pcfc.etks.online.web.model.Register;

/**
 * 
 * 
 * @author Mohtisham.Salahria
 */
@Controller
@RequestMapping(ViewPath.LOGIN)
public class LoginController extends BaseController{

	/**
	 * Custom handler for displaying the form, i.e.: GET requests for
	 * /register/form.jsp
	 * 
	 * @see #form(HttpServletRequest,SessionStatus,Register, BindingResult)
	 * @see WEB-INF/jsp/login.jsp
	 */
	@Autowired
	WebAuthentication webAuthentication;

	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public String form(HttpServletRequest request,
			@ModelAttribute(value = ConstantsAdmin.LOGIN_MODEL_KEY) ClientLogin login) {

		/*if (request.getParameter(ConstantsAdmin.REDIRECT_URL) != null
				&& !request.getParameter(ConstantsAdmin.REDIRECT_URL).equals("")) {

			request.getSession().setAttribute(ConstantsAdmin.REDIRECT_URL,
					request.getParameter(ConstantsAdmin.REDIRECT_URL));

		}*/	

		return ViewUtils.DEFAULT_VIEW;

	}

	@RequestMapping(value = "/form.htm", method = RequestMethod.POST)
	public String form(HttpServletRequest request, SessionStatus status,
			@ModelAttribute(value = ConstantsAdmin.LOGIN_MODEL_KEY) @Valid ClientLogin login, BindingResult errors) {

		if (errors.hasErrors()) {

			for (Object object : errors.getAllErrors()) {
				if (object instanceof FieldError) {
					FieldError fieldError = (FieldError) object;					
				}
				
				if (object instanceof ObjectError) {
					ObjectError objectError = (ObjectError) object;

				}
			}
			
			return ViewUtils.DEFAULT_VIEW;

		} else {
			if (webAuthentication.login(login.getStrUserName(), login.getStrPassword())) {
				if (request.getSession().getAttribute(ConstantsAdmin.REDIRECT_URL) != null
						&& !request.getSession().getAttribute(ConstantsAdmin.REDIRECT_URL).equals("")) {

					String url = (String) request.getSession().getAttribute(ConstantsAdmin.REDIRECT_URL);
					request.getSession().removeAttribute(ConstantsAdmin.REDIRECT_URL);
					return ViewUtils.getRedirect(url);
				} else {
					return ViewUtils.getRedirect(ViewPath.USER_GENERAL_HOME, PageType.SHOW);
				}
			} else {

				String[] messageArgs = new String[] { getMessageSource().getMessage("label.client.login", null,
						RequestContextUtils.getLocale(request)) };
				errors.reject("error.invalid.username.password", messageArgs, null);

				return ViewUtils.DEFAULT_VIEW;
			}
		}
	}

	/*
	 * @RequestMapping(value = ViewPath.LOGOUT) public String
	 * logout(HttpServletRequest request) { // Log the user out
	 * webAuthentication.logout();
	 * 
	 * // Causes the HttpSession to be invalidated HttpSession session =
	 * request.getSession(false); if (session != null) { session.invalidate(); }
	 * // clear the context explicitly SecurityContextHolder.clearContext();
	 * 
	 * return ViewUtils.getRedirect(ViewPath.LOGIN); }
	 */

}
